package com.example.xianji.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.ZhanNei_Adapter;
import com.example.xianji.bean.My_DuiJie;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.CustomProgressDialog;
import com.example.xianji.widgets.MyListview;
import com.example.xianji.widgets.PullToRefreshLayout_two;
import com.example.xianji.widgets.PullToRefreshLayout_two.OnRefreshListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class Main_My_ZhanNei_XingActivity extends Activity implements OnRefreshListener {
    private ImageView my_zhannei_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private ArrayList<My_DuiJie> list = new ArrayList<My_DuiJie>();
    private ZhanNei_Adapter mZhanNei_Adapter;
    private PullToRefreshLayout_two zhannei_PullToRefreshLayout_two;
    private MyListview mylistview;
    private int page = 1;
    private CustomProgressDialog mCustomProgressDialog;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                mylistview.setAdapter(mZhanNei_Adapter);
                zhannei_PullToRefreshLayout_two.refreshFinish(PullToRefreshLayout_two.SUCCEED);
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
            if (msg.what == 2) {
                mZhanNei_Adapter.notifyDataSetChanged();
                zhannei_PullToRefreshLayout_two.loadmoreFinish(PullToRefreshLayout_two.SUCCEED);
            }
            if (msg.what == 3) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.show();
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__my__zhan_nei__xing);
        my_zhannei_back = (ImageView) findViewById(R.id.my_zhannei_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        zhannei_PullToRefreshLayout_two = (PullToRefreshLayout_two) findViewById(R.id.zhannei_PullToRefreshLayout_two);
        mylistview = (MyListview) findViewById(R.id.mylistview);
        my_zhannei_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_My_ZhanNei_XingActivity.this.finish();
            }
        });

        mZhanNei_Adapter = new ZhanNei_Adapter(this, list);
        mCustomProgressDialog = CustomProgressDialog.createDialog(this);
        mCustomProgressDialog.setCanceledOnTouchOutside(false);
        handler.sendEmptyMessageDelayed(3, 1000);
        zhannei();
        zhannei_PullToRefreshLayout_two.setOnRefreshListener(this);
    }

    public void zhannei() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("page", 1);
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.zhannei_xin,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        list.clear();
                        JSONArray array = response.getJSONArray("data");
                        JSONArray array1 = array.getJSONArray(0);
                        if (array1.length() > 0) {
                            for (int i = 0; i < array1.length(); i++) {
                                JSONObject obj = array1.getJSONObject(i);
                                My_DuiJie mMy_DuiJie = new My_DuiJie();

                                mMy_DuiJie.setCreated_at(obj.getString("created_at"));
                                mMy_DuiJie.setTitle(obj.getString("details"));
                                mMy_DuiJie.setId(obj.getInt("id"));
                                list.add(mMy_DuiJie);
                            }
                        } else {
                            Toast.makeText(Main_My_ZhanNei_XingActivity.this, "您还没有站内信哦", Toast.LENGTH_LONG).show();
                        }
                    }
                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
                zhannei_PullToRefreshLayout_two.refreshFinish(PullToRefreshLayout_two.SUCCEED);
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    public void zhannei_more() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("page", page);
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.zhannei_xin,
                params, new Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        JSONArray array1 = array.getJSONArray(0);
                        if (array1.length() == 0) {
                            Toast.makeText(Main_My_ZhanNei_XingActivity.this, "没有更多数据", Toast.LENGTH_LONG).show();
                        } else {
                            for (int i = 0; i < array1.length(); i++) {
                                JSONObject obj = array1.getJSONObject(i);
                                My_DuiJie mMy_DuiJie = new My_DuiJie();
                                mMy_DuiJie.setCreated_at(obj.getString("created_at"));
                                mMy_DuiJie.setTitle(obj.getString("details"));
                                mMy_DuiJie.setId(obj.getInt("id"));
                                list.add(mMy_DuiJie);
                            }
                        }
                    }
                    handler.sendEmptyMessage(2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                zhannei_PullToRefreshLayout_two.loadmoreFinish(PullToRefreshLayout_two.SUCCEED);
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    public void onRefresh(PullToRefreshLayout_two pullToRefreshLayout) {
        page = 1;
        zhannei();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout_two pullToRefreshLayout) {
        page++;
        zhannei_more();
    }
}